<?php
namespace Admin\Controller;
use Admin\Api\BaseApi;
use Admin\Model\UserModel;
use Admin\Model\extendModel;
use Admin\Controller\CommonController;
use Think\Controller;
class QrcodeController extends BaseController {
    
    public function __construct(){
        parent::__construct();
        
    }
    
    public function loadAction($state){
        $callback  = urlencode(C('DOMAIN').U('Sign/in')); //回调地址
        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->_APPID.'&redirect_uri='.$callback.'&response_type=code&scope=snsapi_base&state='.$state.'#wechat_redirect';
        header("Location:".$url);
    }
    /**
     * 显示推广二维码
     */
    public function showSpreadCodeAction(){
        $UserModel =new UserModel();
        $extendModel=new extendModel();
        $common = new CommonController();
        $userid=$this->_userId;
        $ex_where['uid']=$this->_userId;
        $extendInfo=$extendModel->getExtendInfo($ex_where);
        if(empty($userid)){
            $this->error("当前用户未绑定商户!");
        } 
        //二维码路径
        $filename ='extend_qrcode_'.$this->_userId;
        $qrcode_path=PUBLIC_PATH."images/".$filename.".png";
        if(!file_exists($save_path)){
            $common = new CommonController();
            $data = C('DOMAIN_HOME').U('boss/register',array('inviteCode'=>$extendInfo['code']));
            $size=7;
            $fileName ='extend_qrcode_'.$this->_userId;
            $common->planarCodeAction($data, $fileName,$size);
        }
        //二维码最终路径
        $save_path=PUBLIC_PATH."images/extend_poster_".$userid.".jpg";
        //文字
        $name="邀请码:".$extendInfo['code'];
        //海报背景图
        $dst_path=PUBLIC_PATH."assets/img/qrcode.jpg";
        header("content-type:image/jpeg");
        $im = imagecreatefromstring(file_get_contents($dst_path));
        $font_file = PUBLIC_PATH.'assets/css/msyh.ttf';
        $black = imagecolorallocate($im, 0x00, 0x00, 0x00);
        //添加文字
        imagefttext($im, 25, 0,194, 510,$black, $font_file,$name);
        $qrcode = imagecreatefromstring(file_get_contents($qrcode_path));
        
        //合成新图
        imagecopymerge($im,$qrcode,173,173,0,0,301,301,100);
        imagepng($im);
        imagedestroy($im);   
        //生成自助扫描二维码结束
        //$this->assign("filename",$save_path);
        //$this->display("Code/extend");
    }
    /**
     * 收银二维码
     */
    public function showBossCodeAction(){
        $common = new CommonController();
        $userModel=new UserModel();
        $storeParam['belongboss']=$this->_userId;
        $storeParam['userauth']=5;//门店列表
        $storeList = $userModel->getUserList($storeParam);
         for($i=0;$i<count($storeList);$i++){
            $poster_path=PUBLIC_PATH."images/".$storeList[$i]['userid'].".png";
            if(!file_exists($poster_path)){
                //名称
                $where['userid']=$storeList[$i]['userid'];
                $storeItem = $userModel->getUserItem($where);
                $bossId = $storeItem['belongboss'];
                $where['userid']=$bossId;
                $bossItem = $userModel->getUserItem($where);
                
                //自助收费二维码生成
                $fileName ='selfpay_storeid_'.$storeList[$i]['userid'];
                $qrcode_path=PUBLIC_PATH."images/".$fileName.".png";
                //        if(!file_exists($qrcode_path)){
                $data = C('DOMAIN_HOME').U('handle/selfop',array('storeId'=>$storeList[$i]['userid']));
                $size =10;
                $common->planarCodeAction($data, $fileName,$size);
                //生成自助扫描二维码
                $save_path=PUBLIC_PATH."images/selfop_".$storeList[$i]['userid'].".jpg";
                //        if(!file_exists($save_path)){
                $first=$bossItem['nickname'];
                $next=$storeItem['nickname'];
                $name=$first."_".$next;//文字
                $dst_path=PUBLIC_PATH."assets/img/qcode.jpg";//海报背景图
                header("content-type:image/jpeg");
                $im = imagecreatefromstring(file_get_contents($dst_path));
                $font_file = PUBLIC_PATH.'assets/css/msyh.ttf';
                $black = imagecolorallocate($im, 0x00, 0x00, 0x00);
                imagefttext($im, 30, 0, 40, 50,$black, $font_file,$name);
                $qrcode = imagecreatefromstring(file_get_contents($qrcode_path));
                imagecopymerge($im,$qrcode,190,220,0,0,430,430,100);
                imagepng($im,$save_path);
                imagedestroy($im);
            }  
        }
        $this->assign("storelist",$storeList);
       $this->display("Code/cashier");
    }
}